import { admin as AdminApi } from '../../../../../config/request.js';

module.exports = {
    name: 'list',
    data() {
        var validateUsername = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('用户名不能为空'));
            }
            setTimeout(() => {
                if (value.length > 16 || value.length < 6) {
                    callback(new Error('用户名长度为6-16'));
                } else {
                    callback();
                }
            }, 100);
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.adminForm.password2 !== '') {
                    this.$refs.adminForm.validateField('password2');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.adminForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            adminId: '',
            addFormVisible: false,
            adminForm: {
                username: '',
                password: '',
                password2: '',
                realname: '',
                remark: '',
                config: []
            },
            rules: {
                username: [{ validator: validateUsername, trigger: 'blur', required: true }],
                password: [{ validator: validatePass, trigger: 'blur', required: true }],
                password2: [{ validator: validatePass2, trigger: 'blur', required: true }]
            },
            tableData: [],
            selectData: {
                name: '',
                address: '',
                date: ''
            },
            fields: {
                username: {
                    info: {
                        prop: 'username',
                        label: '用户名',
                        sortable: true
                    },
                    filter: {

                    },
                    style: {
                        //width: '100',
                        align: 'center'
                    }
                },
                realname: {
                    info: {
                        prop: 'realname',
                        label: '姓名',
                        sortable: true
                    },
                    filter: {

                    },
                    style: {
                        //width: '100',
                        align: 'center'
                    }
                },
                level: {
                    info: {
                        prop: 'level',
                        label: '权限',
                        sortable: true
                    },
                    filter: {
                        list: [{
                            text: '管理员',
                            value: 1
                        }, {
                            text: '运营者',
                            value: 2
                        }],
                        multiple: false
                    },
                    style: {
                        //width: '100',
                        align: 'center'
                    }
                },
                status: {
                    info: {
                        prop: 'status',
                        label: '状态',
                        sortable: true
                    },
                    filter: {
                        list: [{
                            text: '启用',
                            value: 1
                        }, {
                            text: '禁用',
                            value: 2
                        }],
                        multiple: false
                    },
                    style: {
                        //width: '100',
                        align: 'center'
                    }
                },
                create_time: {
                    info: {
                        prop: 'create_time',
                        label: '创建时间',
                        sortable: true
                    },
                    filter: {

                    },
                    style: {
                        width: '180',
                        align: 'center'
                    }
                },


                action: {
                    info: {
                        prop: 'action',
                        label: '操作',
                        sortable: true
                    },
                    filter: {

                    },
                    style: {
                        width: '260',
                        align: 'center'
                    }
                },
                remark: {
                    info: {
                        prop: 'remark',
                        label: '备注',
                        sortable: true
                    },
                    filter: {

                    },
                    style: {
                        //width: '150',
                        align: 'center'
                    }
                },
            }
        }
    },
    methods: {
        /**
         * 格式化性别
         */
        formatterLevel(item) {
            return item.level == 1 ? '管理员' : '运营者';
        },

        /**
         * 格式化状态
         */
        formatterStatus(item) {
            return item.status == 1 ? '启用' : '禁用';
        },

        filterLevel(value, item) {
            return item.level == value;
        },
        filterStatus(value, item) {
            return item.status == value;
        },
        formatterCreateTime(item) {
            return item.create_time.replace('.000Z', '').replace('T', ' ');
        },
        onDeleteAdmin(admin, index) {
            this.$confirm('是否确认删除该管理员？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this.tableData.splice(index, 1);
                AdminApi.deleteAdmin.call(this, {

                    id: Number(admin.id),
                }, data => {
                    this.$message.success('删除成功');
                })
            }).catch(_ => {});
        },
        /**
         * 更新管理员状态
         * 
         * @param {any} admin 
         */
        onStatusCheckedChange(admin) {
            AdminApi.updataAdminStatus.call(this, {

                id: Number(admin.id),
                status: admin.statusChecked ? 1 : 0
            }, data => {
                admin.status = data.admin.status;
                this.$message.success(data.admin.status == 1 ? '已经启用' : '已经禁用');
            })
        },
        onLevelCheckedChange(admin) {
            AdminApi.updataAdminLevel.call(this, {

                id: Number(admin.id),
                level: admin.levelChecked ? 1 : 0
            }, data => {
                admin.level = data.admin.level;
                this.$message.success('用户权限已经设置为' + (data.admin.level == 1 ? '管理员' : '运营者'));
            })
        },
        onAddAdmin() {
            this.addFormVisible = true;
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            return user && user.level == 1;
        },
        checkSelf(admin) {
            return admin.id == '1' || admin.id == this.adminId;
        },
        /** 提交表单 */
        submitForm(formName) {
            var admin = this[formName];
            var adminInfo = {

                username: admin.username,
                password: admin.password,
                realname: admin.realname,
                remark: admin.remark,
                status: admin.config.indexOf("启用") < 0 ? 0 : 1,
                level: admin.config.indexOf("管理员权限") < 0 ? 0 : 1
            };
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //添加问题
                    AdminApi.createAdmin.call(this, adminInfo,
                        //添加成功
                        (data) => {
                            this.addFormVisible = false;
                            this.adminForm = {
                                adminusername: '',
                                adminpassword: '',
                                adminpassword2: '',
                                adminrealname: '',
                                adminremark: '',
                                config: []
                            }
                            if (!data['success']) {
                                this.$message.error('添加失败 ！！原因是 : ' + data['msg']);
                                return;
                            }
                            const newAdmin = data['admin'];
                            Object.assign(newAdmin, {
                                statusChecked: newAdmin.status == 1,
                                levelChecked: newAdmin.level == 1,
                            });
                            let create_time = newAdmin.create_time;
                            create_time = create_time.replace('T', ' ');
                            create_time = create_time.substring(0, create_time.lastIndexOf('.'));
                            newAdmin.create_time = create_time;
                            this.tableData.push(newAdmin);
                            this.$message.success('添加' + (newAdmin.level == 1 ? '管理员' : '运营者') + '成功 ！！');

                        })
                } else {
                    return false;
                }
            });
        },
        resetForm() {
            this.adminForm = {
                adminusername: '',
                adminpassword: '',
                adminpassword2: '',
                adminrealname: '',
                adminremark: '',
                config: []
            }
        },

    },
    created: function() {

        AdminApi.getAdmin.call(this, data => {
            data.admin.forEach(admin => {
                Object.assign(admin, { statusChecked: admin.status == 1, levelChecked: admin.level == 1 });
            });
            this.tableData = data.admin;
        })

        this.adminId = this.$store.state.user.userinfo.id;
    }
}